package com.lishide.scrollrecyclerview;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.lishide.scrollrecyclerview.utils.NetUtil2;


public class NetBroadcastReceiver extends BroadcastReceiver {

    public NetEvevt evevt = BaseActivity.evevt;
    public int NET_ETHERNET = 1;
    public int NET_WIFI = 2;
    public int NET_NOCONNECT = 0;
    public static final String WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED";

    public static final String ACTION_TETHER_STATE_CHANGED =
            "android.net.ethernet.ETHERNET_STATE_CHANGED";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        // 如果相等的话就说明网络状态发生了变化
        int netWorkState = -1;


        try{

            if(NetUtil2.isEthernetConnect(context)){
                netWorkState=1;



            }


        }catch (Exception e){


        }
        if(netWorkState!=1) {
            if (ACTION_TETHER_STATE_CHANGED.equals(intent.getAction())) {

                if (NetUtil2.isEthernetConnect(context)) {
                    netWorkState = 1;


                }else{
                    netWorkState = 3;
                }

            }


            if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {

                // 接口回调传过去状态的类型


                if (NetUtil2.isNetWorkAvailable(context)) {
                    netWorkState = 1;

                } else if (NetUtil2.isWifiConnect(context)) {
                    netWorkState = 2;

                }


            }

//            if (WIFI_AP_STATE_CHANGED_ACTION.equals(intent.getAction())) {
//
//                int cstate = intent.getIntExtra(EXTRA_WIFI_AP_STATE, -1);
//                if (cstate == 13) {
//                    netWorkState = 1;
//                } else {
//
//
//                    if (NetUtil2.isNetWorkAvailable(context)) {
//                        netWorkState = 1;
//
//                    } else if (NetUtil2.isWifiConnect(context)) {
//                        netWorkState = 2;
//
//                    }
//
//
//                }
//
//            }
        }

        evevt.onNetChange(netWorkState);


    }


    private int isNetworkAvailable(Context context) {
        ConnectivityManager connectMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ethNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


        if (ethNetInfo != null && ethNetInfo.isConnected()) {
            return NET_ETHERNET;
        } else if (wifiNetInfo != null && wifiNetInfo.isConnected()) {
            return NET_WIFI;
        } else {
            return NET_NOCONNECT;
        }
    }


    // 自定义接口
    public interface NetEvevt {
        public void onNetChange(int netMobile);
    }
}
